package cn.demo.d01;

import java.util.Arrays;
import java.util.Scanner;

public class Demo07 {

    public static void main(String[] args) {
        // test41();
        // System.out.println("--------------------------------");
        // test42();
        // System.out.println("--------------------------------");
        // test43();
        // System.out.println("--------------------------------");
        // test44();
        // System.out.println("--------------------------------");
        // test45();
        // System.out.println("--------------------------------");
        // test46();
        // System.out.println("--------------------------------");
        // test47();
        // System.out.println("--------------------------------");
        // test48();
        // System.out.println("--------------------------------");
        // test49();
        // System.out.println("--------------------------------");
        test50();
        System.out.println("--------------------------------");
    }

    /*
        // 需求：
        // 把整数上的每一位都添加到数组当中
        // 反向推导
    * */
    private static void test41() {
        System.out.println("请输入一个整数：");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int temp = num;
        int count = 0;
        while (num != 0){
            num /= 10;
            count++;
        }
        int[] arr = new int[count];
        for (int i = arr.length-1; i >= 0; i--) {
            arr[i] = temp % 10;
            temp /= 10;
        }
        System.out.println("数组中的元素为：" + Arrays.toString(arr));
    }

    /*  某系统的数字密码（大于0）。比如1983，采用加密方式进行传输，
        规则如下：
            每位数加上5
            再对10求余，
            最后将所有数字反转，
            得到一串新数。
        按照以上规则进行解密：
        比如1983加密之后变成8346，解密之后变成1983
    */
    private static void test42() {
        System.out.println("请输入要解密的密码: ");
        Scanner sc = new Scanner(System.in);
        String pwd = sc.next();
        int[] arr = new int[pwd.length()];
        for (int i = 0; i < pwd.length(); i++) {
            arr[i] = pwd.charAt(i)-'0';
        }
        for (int i = 0,j=arr.length-1; i < j; i++,j--) {
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
        //3.由于加密是通过对10取余的方式进行获取的
        //所以在解密的时候就需要判断，0~4之间+10  5~9数字不变
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] >= 0 && arr[i] <= 4) {
                arr[i] = arr[i] + 10;
            }
        }
        //4.每一位减5
        for (int i = 0; i < arr.length; i++) {
            arr[i] = arr[i] - 5;
        }
        int result = 0;
        for (int i = 0; i < arr.length; i++) {
            result = result * 10 + arr[i];
        }
        System.out.println(Arrays.toString(arr));
        System.out.println(result);
    }

    /*
    已知用户名和密码，请用程序实现模拟用户登录。总共给三次机会，登录之后，给出相应的提示
    * */
    private static void test43(){
        Scanner sc = new Scanner(System.in);
        String dbUsername = "admin";
        String dbPassword = "123456";
        int count = 3;
        for (int i=0;i<count;i++) {
            System.out.println("请输入用户名: ");
            String username = sc.next();
            System.out.println("请输入密码: ");
            String password = sc.next();
            if (dbUsername.equals(username) && dbPassword.equals(password)) {
                System.out.println("登录成功");
                break;
            }
            if (i == count - 1) {
                System.out.println("你已登录失败"+count+"次，账户已经被锁定,请明天再试");
            }else{
                System.out.println("登录失败，你还有" + (count-1-i) + "次机会");
            }
        }
    }

    /*
    键盘录入一个字符串，使用程序实现在控制台遍历该字符串
    * */
    private static void test44() {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串: ");
        String str = sc.next();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            System.out.println(c);
        }
    }

    /*
    键盘录入一个字符串，统计该字符串中大写字母字符，小写字母字符，数字字符出现的次数(不考虑其他字符)
    * */
    private static void test45() {
        int bigCount = 0;
        int smallCount = 0;
        int numCount = 0;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串: ");
        String str = sc.next();
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (c >= 'A' && c <= 'Z') {
                bigCount++;
            }else if (c >= 'a' && c <= 'z'){
                smallCount++;
            }else if (c>= '0' && c <= '9') {
                numCount++;
            }
        }
        System.out.println("输入的原始字符串: " + str);
        System.out.println("大写字母字符出现的次数为: " + bigCount);
        System.out.println("小写字母字符出现的次数为: " + smallCount);
        System.out.println("数字字符出现的次数为: " + numCount);
    }

    /*
    定义一个方法，实现字符串反转。键盘录入一个字符串，调用该方法后，在控制台输出结果
    例如，键盘录入 abc，输出结果 cba
    * */
    private static void test46(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串: ");
        String str = sc.next();
        StringBuilder sb = new StringBuilder();
        for (int i = str.length()-1; i >= 0; i--) {
            sb.append(str.charAt(i));
        }
        System.out.println("反转后的字符串: " + sb);
    }

    /*
    把2135变成：零佰零拾零万贰仟壹佰叁拾伍元
	把789变成：零佰零拾零万零仟柒佰捌拾玖元
    * */
    private static void test47(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入金额: ");
        int money;
        while (true) {
            System.out.println("请录入一个金额");
            money = sc.nextInt();
            if (money >= 0 && money <= 9999999) {
                break;
            } else {
                System.out.println("金额无效");
            }
        }
        StringBuilder sb = new StringBuilder();
        while (money != 0){
            int ge = money % 10;
            String bigStr = getCapitalNumber(ge);
            sb.insert(0,bigStr);
            money /= 10;
        }
        System.out.println(sb);
        //3.在前面补0，补齐7位
        int count = 7-sb.length();
        for (int i = 0; i < count; i++) {
            sb.insert(0,"零");
        }
        //4.插入单位
        //定义一个数组表示单位
        String[] arr = {"佰","拾","万","仟","佰","拾","元"};
        StringBuilder resultSb = new StringBuilder();
        for (int i = 0; i < sb.length(); i++) {
            resultSb.append(sb.charAt(i)).append(arr[i]);
        }
        System.out.println(resultSb);
    }

    //定义一个方法把数字变成大写的中文
    //1 -- 壹
    public static String getCapitalNumber(int number) {
        String[] arr = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        return arr[number];
    }

    /*
    需求：以字符串的形式从键盘接受一个手机号，将中间四位号码屏蔽
    最终效果为：`131****9468`
    * */
    private static void test48(){
        Scanner sc = new Scanner(System.in);
        String phone;
        while (true) {
            System.out.println("请输入一个手机号: ");
            phone = sc.next();
            if(phone.length() == 11){
                break;
            }
            System.out.println("手机号格式不正确，请重新输入");
        }
        //131****9468
        String begin = phone.substring(0,3);
        String end = phone.substring(7);
        System.out.println(phone);
        System.out.println(begin+"****"+end);
    }

    /*
    需求1：键盘录入一个 字符串，如果字符串中包含（TMD），则使用***替换
    * */
    private static void test49(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个字符串: ");
        String str = sc.next();
        String oldStr = str;
        String[] sensitiveWords = {"TMD","DM"};
        for (int i = 0; i < sensitiveWords.length; i++) {
            str = str.replace(sensitiveWords[i], "***");
        }
        System.out.println(oldStr);
        System.out.println(str);
    }

    /**
     身份证的每一位都是有固定的含义：
        1、2位：省份
        3、4位：城市
        5、6位：区县
        7-14位：出生年、月、日
        15、16位：所在地派出所
        17位：性别（奇数男性，偶数女性）
        18位：个人信息码（随机产生）
     要求打印内容方式如下：
     人物信息为：
     出生年月日：XXXX年X月X日
     性别为：男/女
     * */
    private static void test50(){
        Scanner sc = new Scanner(System.in);
        String idCard;
        while (true) {
            System.out.println("请输入一个身份证: ");
            idCard = sc.next();
            if (idCard.length() == 18){
                break;
            }
            System.out.println("身份证格式不对");
        }
        String provinceCode = idCard.substring(0, 2);
        String cityCode = idCard.substring(2, 4);
        String areaCode = idCard.substring(4, 6);
        System.out.println("所在地区为: " + getProvinceByCode(provinceCode)+"省"+getCityByCode(cityCode)+"市"+getAreaByCode(areaCode)+"区");
        System.out.println("出生年月日："+idCard.substring(6,10)+"年"+idCard.substring(10,12)+"月"+idCard.substring(12,14)+"日");
        System.out.println("性别为："+(idCard.charAt(16)%2==0?"女":"男"));
    }

    private static String getAreaByCode(String code) {
        String[] areas = {"晋安","花都","商河"};
        String[] areasId = {"11","12","13"};
        for (int i = 0; i < areasId.length; i++) {
            if (areasId[i].equals(code)){
                return areas[i];
            }
        }
        return "未知";
    }

    private static String getCityByCode(String code) {
        String[] cities = {"福州","广州","济南"};
        String[] citiesId = {"01","02","03"};
        for (int i = 0; i < citiesId.length; i++) {
            if (citiesId[i].equals(code)){
                return cities[i];
            }
        }
        return "未知";
    }

    private static String getProvinceByCode(String code){
        String[] provinces = {"福建","广东","山东"};
        String[] provincesId = {"35","36","37"};
        for (int i = 0; i < provincesId.length; i++) {
            if (provincesId[i].equals(code)){
                return provinces[i];
            }
        }
        return "未知";
    }
}

